﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Labb2
{
    class SplitterParticle
    {
        int m_seed;
        Texture2D dogeTexture;
        Random rand;
        Camera m_camera;
        Vector2 m_startPosition;
        Vector2 m_dogePosition;
        Vector2 m_velocity;
        Vector2 m_acceleration;
        float m_diameter;

        public SplitterParticle(Vector2 startingPosition, int seed, Texture2D texture, Camera camera) 
        {
            m_seed = seed;
            rand = new Random(seed);
            m_camera = camera;
            dogeTexture = texture;
            m_diameter = 0.1f;
            //m_startPosition = new Vector2(250f, 300f);
            m_startPosition = startingPosition;
            m_dogePosition = new Vector2(m_startPosition.X, m_startPosition.Y);
            
            m_velocity = new Vector2((float)(rand.NextDouble() * 2.0 - 1.0), (float)(rand.NextDouble() * 2.0 - 1.0));
            //m_velocity = new Vector2((float)(rand.NextDouble() * 0.01 - 0.005), (float)(rand.NextDouble() * 0.01 - 0.005));
            m_velocity.Normalize();

            m_acceleration = new Vector2(0.0f, 10f);

            float min = 1.5f;
            float max = 3.0f;
            //float min = 0.05f;
            //float max = 0.1f;
            float particleSpeed = min + ((float)(rand.NextDouble()) * (max - min));

            m_acceleration *= particleSpeed;
            //System.Diagnostics.Debug.WriteLine(m_camera.GetVisualCoords(m_dogePosition));
        }

        internal void Update(GameTime gameTime) 
        {
            //Vector2 randomDirection = new Vector2((float)rand.NextDouble() - 0.5f, (float)rand.NextDouble() - 0.5f);
            //randomDirection.Normalize();

            m_dogePosition += (m_velocity / 500) * gameTime.ElapsedGameTime.Milliseconds / 15;
            m_velocity += m_acceleration * gameTime.ElapsedGameTime.Milliseconds / 5000;
            
            //System.Diagnostics.Debug.WriteLine(gameTime.ElapsedGameTime.Milliseconds);
            
            //m_dogePosition = m_dogePosition + m_velocity * gameTime.ElapsedGameTime.Milliseconds / 15;
            //m_velocity = m_velocity + m_acceleration * (gameTime.ElapsedGameTime.Milliseconds - 1000);
           

            //Vector2 newVelocity = new Vector2();
            //System.Diagnostics.Debug.WriteLine("doge Number: " + m_seed + "Position: " + m_dogePosition);
            //newVelocity.X = gameTime.ElapsedGameTime.Seconds * 

        }

        internal void Draw(SpriteBatch spritebatch) 
        {
            int xCoord = (int)m_camera.GetVisualCoords(m_dogePosition).X;
            int yCoord = (int)m_camera.GetVisualCoords(m_dogePosition).Y;
            Vector2 size = m_camera.GetVisualCoords(new Vector2(m_diameter, m_diameter));
            int width = (int)size.X;
            int height = (int)size.Y;

            //Rectangle dogeRectangle = new Rectangle((int)m_camera.GetVisualCoords(m_dogePosition).X, (int)m_camera.GetVisualCoords(m_dogePosition).Y, 114 / 5, 129 / 5);
            Rectangle dogeRectangle = new Rectangle(xCoord, yCoord, width, height);
            spritebatch.Draw(dogeTexture, dogeRectangle, Color.White);            
        }
    }
}
